package org.springframework.cloud.bus;

import org.springframework.cloud.bus.event.RemoteApplicationEvent;
import org.springframework.cloud.function.json.JacksonMapper;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @author ahaschool
 * @date 2021-06-09 13:50
 */

public class RedisBusBridge implements BusBridge {


    private final StringRedisTemplate redisTemplate;
    private final BusProperties properties;
    private final JacksonMapper jacksonMapper;

    public RedisBusBridge(StringRedisTemplate redisTemplate, BusProperties properties, JacksonMapper jacksonMapper) {
        this.redisTemplate = redisTemplate;
        this.properties = properties;
        this.jacksonMapper = jacksonMapper;
    }

    @Override
    public void send(RemoteApplicationEvent event) {
        String destination = properties.getDestination();
        String value = jacksonMapper.toString(event);
        redisTemplate.convertAndSend(destination, value);
    }
}
